package com.zp.demo.repush.service;

import com.zp.demo.repush.vo.BaseRePushVo;
import com.zp.demo.repush.vo.ScheduledProperties;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

public abstract class AbstractScheduled {
    /**
     * 从 DB获取定时任务列表
     * @return
     */
    public abstract List<BaseRePushVo> getScheduledFromDb();

    /**
     * 添加到执行队列
     * @param rePushVos
     * @param redisTemplate
     * @return
     */
    public boolean add2ScheduledQueue(List<BaseRePushVo> rePushVos, RedisTemplate<Object, Object> redisTemplate){
        rePushVos.forEach(x ->{
            redisTemplate.opsForList().leftPush(ScheduledProperties.SCHEDULED_KEY,x);
        });
        return true;
    }

    /**
     * 开启定时任务
     * @param redisTemplate
     * @return
     */
    public boolean startScheduled( RedisTemplate<Object, Object> redisTemplate){
        add2ScheduledQueue(getScheduledFromDb(),redisTemplate);
        return true;
    }
}
